<% /**
	* GraphOptions.jsp
	* 
	* for graphing
	*
	* @author Victor Vazquez
	*/
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="b1w1nn1ng.crossf1t.gui.*, b1w1nn1ng.crossf1t.business.*, java.util.*, b1w1nn1ng.crossf1t.db.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title> Graph Options</title>
		<jsp:include page="/detectBrowser.jsp" />	
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
		<script type="text/javascript" src="highcharts.js"></script>
	</head>
	<body>
	 <% //this is a convienience variable: If it is true, then we have sufficient information to show a graph
    boolean ready = false; 
  	%>
		<div id="container">
		<h1> Graphing </h1> <hr />
		<jsp:include page="/navbar.jsp" />	
		Select a workout type:
		<form action="./GraphOptions.jsp" method="get" id = "myForm">
		<select name="primary" onchange="this.form.submit();"> 
			<% 
			//get the different values and add them as select options
			String[] primaries = Database.getPrimarySeeList(false);
			
			//Print the "Categories" option at the top of the menu if we haven't selected one yet
			if(request.getParameter("primary") == null) {
				out.println("<option>Categories</option>");
			}
			
			//for each entry in the string array, prints it as an option in format:
			// <option value="entry">entry</option>
			for(String entry:primaries) {
				
				// If an option was selected last time, we do this check to keep it selected:
				if(request.getParameter("primary") != null) {
					if(request.getParameter("primary").equals(entry))
						out.println("<option selected value=\""+entry+"\">"+entry+"</option>");
					else
						out.println("<option value=\""+entry+"\">"+entry+"</option>");
				// Else, just print the options
				} else {
					out.println("<option value=\""+entry+"\">"+entry+"</option>");
				}
			}
			%>
		</select>
		<%
		
		// If a primary category was selected, we may have to display the secondary menu. Check
		// if we have to here:
		if(request.getParameter("primary") != null) {
			String[] secondary = Database.getSecondarySeeList(request.getParameter("primary"));
			
			//if the secondary seeList is null, we're done, there is no secondary.
			if(secondary == null) {
				ready = true;
			} else {
				//else, there is a secondary: here's the select menu for it.
				out.println("<select name=\"secondary\" onchange=\"this.form.submit();\">");
				//Print the "Excercise" option at the top of the menu if we haven't selected one yet
				//OR if primary changed since last time.
				if(request.getParameter("secondary") == null) {
					out.println("<option>Excercise</option>");
					ready = false;
				} else {
					boolean found = false;
					//check if primary changed by seeing if selected secondary is in the list still
					for(int i = 0; i < secondary.length; i++) {
						if(secondary[i].equals(request.getParameter("secondary"))) {
							found = true;
							ready = true;
							break;
						}
					}
					if(!found) {
						out.println("<option>Excercise</option>");
						ready = false;
					}
				}
				
				//for each entry in the string array, prints it as an option in format:
				// <option value="entry">entry</option>
				for(String entry:secondary) {
					
					// If an option was selected last time, we do this check to keep it selected:
					if(request.getParameter("secondary") != null) {
						if(request.getParameter("secondary").equals(entry))
							out.println("<option selected value=\""+entry+"\">"+entry+"</option>");
						else
							out.println("<option value=\""+entry+"\">"+entry+"</option>");
					// Else, just print the options
					} else {
						out.println("<option value=\""+entry+"\">"+entry+"</option>");
					}
				}
				out.println("</select>");
			}
		}
		%>		
	<br>	
		
		<br/>With Whom do you want to compare:<br/>
	<script language ='JavaScript' TYPE="text/javascript">
		var temp = document.getElementById("myForm");
		var myArray=new Array();
		var anotherArray=new Array();
		var oldHTML;
		var friend_name;
		var newHTML;

		<%
		User user = (User) session.getAttribute("user");
		session.setAttribute("user", user);
		//TODO UNCOMMENT THIS//ArrayList<Integer> friends = user.getFriends();
		ArrayList<String> friendNames = new ArrayList<String> ();
		//int size = friends.size();
		int size = 3;
		friendNames.add("john");
		friendNames.add("billy");
		friendNames.add("James");
		int j = 0;
		//for(int i= 0; i<size; i++)
		//{
			//friendNames.add((Database.getUserByID(friends.get(i).getId())).getFirstName());
		//}
		for(int i=0;i<size; i++)
		{ %>
		myArray[<%=i%>] = "<%=friendNames.get(i)%>";
			
		<% }
	%>
	for(var i = 0; i < <%=friendNames.size()%> ; i++)
	{	
		oldHTML = temp.innerHTML;
		friend_name = myArray[i];
		//friend_id = anotherArray[i];
		newHTML = oldHTML+"<input type = 'checkbox' name =" +
			"'friend' value =" + friend_name +">"+friend_name+"<br />";
		temp.innerHTML = newHTML;
	}
	var graph= <%=ready%>;
	var nothing = "this.form.action='VisualGraph.jsp' value ='Graph!' ";
	//var nothing = "this.form.action= 'this.Graph()' value ='Graph!' ";
		if(graph)
		{
		oldHTML = temp.innerHTML;
		newHTML = oldHTML + 	"<input type= 'submit' onclick=" + nothing + "/>";
		temp.innerHTML = newHTML;
		}
</script>
</form><br>
	</div>
</html>



